package com.bot.adms.dao.impl;

import com.bot.adms.dao.IApplyContentSpScDetailDao;
import com.bot.adms.model.entity.ApplyContentSpScDetail;
import com.bot.adms.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/*
 * 投放申请分屏明细持久层接口实现
 * @author 刘孜涵
 * */
public class ApplyContentSpScDetailDaoImpl implements IApplyContentSpScDetailDao {

    private Connection conn;
    private Statement stmt;
    private ResultSet rs;
    /*
      添加投放申请分屏明细
      @param applyContentSpScDetail 投放申请分屏明细信息 包含：投放申请分屏明细标识，类型，分屏号
     * @return Integer 添加成功数量
     */
    @Override
    public Integer insertApplyContentSpScDetail(ApplyContentSpScDetail applyContentSpScDetail) {
        try {
            //创建连接
            conn = JdbcUtils.getConnection();
            //根据连接获取statemen对象
            stmt = conn.createStatement();
            //执行sql
            String sql = "INSERT INTO `bo_adms`.`apply_content_sp_sc_detail` ( `type`, `sc_id`,`apply_content_id`)" +
                    " VALUES ("+applyContentSpScDetail.getType()+","+applyContentSpScDetail.getScId()+","+applyContentSpScDetail.getApplyContentId()+")";
            int count = stmt.executeUpdate(sql);
            return count;
        }catch (SQLException e){
            throw new RuntimeException(e);
        }finally {
            JdbcUtils.close(conn, stmt, rs);
        }
    }

    /*
    修改投放申请分屏明细
    @param applyContentSpScDetail 投放申请分屏明细信息 包含：投放申请分屏明细标识，类型，分屏号
    * @return Integer 修改成功数量
     */
    @Override
    public Integer updateApplyContentSpScDetail(ApplyContentSpScDetail applyContentSpScDetail) {
        try {
            //创建连接
            conn = JdbcUtils.getConnection();
            //根据连接获取statemen对象
            stmt = conn.createStatement();
            //执行sql
            String sql = "UPDATE `bo_adms`.`apply_content_sp_sc_detail` SET " +
                    "`type`="+applyContentSpScDetail.getType()+"," +
                    "`sc_id`= "+applyContentSpScDetail.getScId()+
                    " WHERE `id` = "+applyContentSpScDetail.getId()+"";
            int count = stmt.executeUpdate(sql);
            return count;
        }catch (SQLException e){
            throw new RuntimeException(e);
        }finally {
            JdbcUtils.close(conn, stmt, rs);
        }
    }

    /*
     删除投放申请分屏明细
     @param   id 投放申请分屏明细标识
    * @return Integer 删除成功数量
    */
    @Override
    public Integer deleteApplyContentSpScDetail(Long id) {
        try {
            //创建连接
            conn = JdbcUtils.getConnection();
            //根据连接获取statemen对象
            stmt = conn.createStatement();
            //执行sql
            String sql = "DELETE FROM `bo_adms`.`apply_content_sp_sc_detail` WHERE `id` = "+ id;
            int count = stmt.executeUpdate(sql);
            return count;
        }catch (SQLException e){
            throw new RuntimeException(e);
        }finally {
            JdbcUtils.close(conn, stmt, rs);
        }
    }

    /*
   查找投放申请分屏明细信息
   @param id 投放申请分屏明细标识
   * @return applyContentSpScDetail 投放申请分屏明细信息 包含：投放申请分屏明细标识，类型，分屏号
   */
    @Override
    public ApplyContentSpScDetail getApplyContentSpScDetailById(Long id) {
        ApplyContentSpScDetail applyContentSpScDetail = new ApplyContentSpScDetail();
        try {
            //创建连接
            conn = JdbcUtils.getConnection();
            //根据连接获取statemen对象
            stmt = conn.createStatement();
            //执行sql
            String sql = "SELECT id,type,sc_id,apply_content_id FROM `bo_adms`.`apply_content_sp_sc_detail` WHERE `id` = "+id+"";
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                int type = rs.getInt("type");
                int scId = rs.getInt("sc_id");
                long applyContentId = rs.getLong("apply_content_id");
                applyContentSpScDetail.setType(type);
                applyContentSpScDetail.setScId(scId);
                applyContentSpScDetail.setApplyContentId(applyContentId);
            }
        }catch (SQLException e){
            throw new RuntimeException(e);
        }finally {
            JdbcUtils.close(conn, stmt, rs);
        }
        return applyContentSpScDetail;
    }

    /*
   查询投放申请分屏明细
   @param applyContentSpScDetail 投放申请分屏明细信息 包含：投放申请分屏明细标识，类型，分屏号
   * @return ApplyContentSpScDetail 投放申请分屏明细信息集合 包含：投放申请分屏明细标识，类型，分屏号
   */
    @Override
    public List<ApplyContentSpScDetail> searchApplyContentSpScDetail(ApplyContentSpScDetail applyContentSpScDetail) {
        List<ApplyContentSpScDetail> applyContentSpScDetailList = new ArrayList<>();
        try {
            //创建连接
            conn = JdbcUtils.getConnection();
            //根据连接获取statemen对象
            stmt = conn.createStatement();
            //执行sql
            String sql = "SELECT id,type,sc_id,apply_content_id FROM `bo_adms`.`apply_content_sp_sc_detail` WHERE 1=1";
            if(applyContentSpScDetail.getType()!=null){
                sql += " AND `type`="+applyContentSpScDetail.getType();
            }
            if(applyContentSpScDetail.getScId()!=null){
                sql += " AND `sc_id`="+applyContentSpScDetail.getScId();
            }
            if (applyContentSpScDetail.getApplyContentId()!=null){
                sql += " AND `apply_content_id`="+applyContentSpScDetail.getApplyContentId();
            }
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                ApplyContentSpScDetail newapplyContentSpScDetail = new ApplyContentSpScDetail();
                long id = rs.getLong("id");
                int type = rs.getInt("type");
                int scId = rs.getInt("sc_id");
                long applyContentId = rs.getLong("apply_content_id");
                newapplyContentSpScDetail.setId(id);
                newapplyContentSpScDetail.setType(type);
                newapplyContentSpScDetail.setScId(scId);
                newapplyContentSpScDetail.setApplyContentId(applyContentId);
                applyContentSpScDetailList.add(newapplyContentSpScDetail);
            }
        }catch (SQLException e){
            throw new RuntimeException(e);
        }finally {
            JdbcUtils.close(conn, stmt, rs);
        }
        return applyContentSpScDetailList;
    }
}
